/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import xowl.utils.config.Configuration;
import xowl.utils.config.Section;

/**
 *
 * @author Laurent WOUTERS
 */
public class WorkbenchView {
    private String id;
    private String fileView;
    private String fileCMRules;
    private String fileMVRules;
    private String fileVVRules;
    private Map<String, ExecutionEntry> execEntries;
    private xowl.lang.owl2.Ontology ontoViewModel;
    private xowl.interpreter.RuleSet rulesCM;
    private xowl.interpreter.RuleSet rulesMV;
    private xowl.interpreter.RuleSet rulesVV;
    private xowl.gmi.model.cvssl.ViewDefinition root;
    private String name;
    
    public String getID() { return id; }
    public String getName() { return name; }
    public xowl.gmi.model.cvssl.ViewDefinition getDefinition() { return root; }
    public xowl.lang.owl2.Ontology getOntology() { return ontoViewModel; }
    public xowl.interpreter.RuleSet getCMRuleset() { return rulesCM; }
    public xowl.interpreter.RuleSet getMVRuleset() { return rulesMV; }
    public xowl.interpreter.RuleSet getVVRuleset() { return rulesVV; }
    public Collection<ExecutionEntry> getExecEntries() { return execEntries.values(); }
    public ExecutionEntry getExecEntry(String id) { return execEntries.get(id); }
    
    public WorkbenchView(Configuration config, String id) {
        Section section = config.getSection(id);
        this.id = id;
        this.fileView = section.getValue("view");
        this.fileCMRules = section.getValue("cm");
        this.fileMVRules = section.getValue("mv");
        this.fileVVRules = section.getValue("vv");
        this.execEntries = new HashMap<>();
        for (String exec : section.getValues("exec"))
            execEntries.put(exec, new ExecutionEntry(section, exec));
    }
    
    public void load(xowl.interpreter.Repository repository) {
        this.ontoViewModel = repository.loadOntology(fileView, xowl.interpreter.OWL2Syntax.Functional);
        this.rulesCM = repository.loadRules(fileCMRules, xowl.interpreter.OWL2Syntax.Functional);
        this.rulesMV = repository.loadRules(fileMVRules, xowl.interpreter.OWL2Syntax.Functional);
        this.rulesVV = repository.loadRules(fileVVRules, xowl.interpreter.OWL2Syntax.Functional);
        this.root = xowl.gmi.model.cvssl.ViewDefinition.getAll(repository, ontoViewModel).iterator().next();
        this.name = root.getDescription();
    }
    
    @Override public String toString() { return name; }
}
